package net.gz01.wallet.web.service;


import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;

import net.gz01.util.ZODate;
import net.gz01.util.ZORegex;
import net.gz01.util.ZOString;
import net.gz01.wallet.common.ZOIDGenerate;
import net.gz01.wallet.web.config.SystemEnum.PayOrderItemStatus;
import net.gz01.wallet.web.config.SystemEnum.PayOrderStatus;
import net.gz01.wallet.web.model.Merch;
import net.gz01.wallet.web.model.PayOrder;
import net.gz01.wallet.web.model.PayOrderItem;

public class PayOrderService {

	public List<String> batchPay(List<String[]> data,String merchCode){
		List<String> errorMsg = new ArrayList<>();
		Long amount;
		String accountName;
		String accountNo;
		String accountType;
		String createDateTime = ZODate.getDateTime();
		List<PayOrderItem> saveList = new ArrayList<>();
		Merch merch = Merch.dao.selectByMerchCode(merchCode);
		String excelAmount;
		DecimalFormat df = new DecimalFormat("0.00");
		//生成统计单
		PayOrder order = new PayOrder();
		order.setBatchNo(getBatchNo());
		order.setTotalAmount(0L);
		order.setSuccessNumber(0);
		order.setMerchCode(merchCode);
		order.setOrgCode(merch.getOrgCode());
		order.setCreateDateTime(ZODate.getDateTime());
		order.setOrderStatus(PayOrderStatus.WaitOne.getValue());
		//第一行是标题
		String[] rowData;
		for (int i = 3; i < data.size(); i++) {
			rowData = data.get(i);
			if(StrKit.notBlank(rowData[0]) && getDecimalLength(rowData[0]) > 2) {
				errorMsg.add(i + 1 + "行:金额格式校验不通过,请确保小数点不能大于2位");
				continue;
			}

			excelAmount = df.format(new Double(rowData[0]));
			amount = new BigDecimal(excelAmount).multiply(new BigDecimal(100)).longValue();
			accountName = rowData[1].trim();
			accountNo = rowData[2].trim();
			accountType = rowData[3].trim();

			if(StrKit.isBlank(accountName)) {
				errorMsg.add(i + 1 + "行:户名非空校验不通过");
				continue;
			}

			if(ZORegex.isPositiveInt(accountNo) == false){
				errorMsg.add(i + 1 + "行:行银行卡号校验不通过");
				continue;
			}

			if(StrKit.isBlank(accountType)) {
				errorMsg.add(i + 1 + "行:收款方类型校验不通过");
				continue;
			}

			PayOrderItem item = new PayOrderItem();
			item.setOrgCode(merch.getOrgCode());
			item.setMerchCode(merchCode);
			item.setOrderNo(ZOIDGenerate.getSixOrderNo());
			item.setAmount(Long.valueOf(amount));
			item.setAccountName(accountName);
			item.setAccountNumber(accountNo);
			item.setBatchNo(order.getBatchNo());
			item.setOrderStatus(PayOrderItemStatus.WaitOne.getValue());
			item.setCreateDateTime(createDateTime);
			if(StrKit.notBlank(rowData[6])) {
				item.setBankName(rowData[6]);
			} else {
				item.setBankName("");
			}

			if(StrKit.notBlank(rowData[7])) {
				item.setOpeningBankNo(rowData[7]);
			} else {
				item.setOpeningBankNo("");
			}

			if("企业".equals(accountType)) {
				if(StrKit.isBlank(rowData[4])) {
					errorMsg.add(i + 1 + "行:[开户行省份]非空校验不通过");
					continue;
				}
				if(StrKit.isBlank(rowData[5])) {
					errorMsg.add(i + 1 + "行:[开户行城市]非空校验不通过");
					continue;
				}
				if(StrKit.isBlank(rowData[6])) {
					errorMsg.add(i + 1 + "行:[开户行支行行名]非空校验不通过");
					continue;
				}
				if(StrKit.isBlank(rowData[7])) {
					errorMsg.add(i + 1 + "行:[开户行支行行号]非空校验不通过");
					continue;
				}
				item.setIsCompany(true);
				item.setProvince(rowData[4]);
				item.setCity(rowData[5]);
			} else {
				item.setIsCompany(false);
				item.setProvince("");
				item.setCity("");
			}

			if(rowData.length == 9) {
				item.setRemark(rowData[8]);
			}

			saveList.add(item);
			order.setTotalAmount(order.getTotalAmount().longValue() + item.getAmount().longValue());
		}

		if(errorMsg.isEmpty()) {
			String checkMsg = check(data.get(1), order.getTotalAmount(), saveList.size());
			if(StrKit.notBlank(checkMsg)) {
				errorMsg.add(checkMsg);
			}else {
				order.setTotalNumber(saveList.size());
				order.save();
				Db.batchSave(saveList, 100);
			}
		}
		return errorMsg;
	}

	private String getBatchNo(){
		String batchNo = Db.queryFirst("select nextval('batchNo')").toString();
		batchNo = ZOString.appendZeroToLeft(batchNo, 4);
		batchNo = "P" + ZODate.getDateNumber() + ZOIDGenerate.getIntRandomCode(6) + batchNo;
		return batchNo;
	}

	/**
	 * 获取小数点后长度
	 * @param src
	 * @return
	 */
	private int getDecimalLength(String src) {
		src = src.substring(src.lastIndexOf(".")+1);
		return src.length();
	}

	private String check(String[] rowData,Long totalAmount,Integer total) {
		String res = "";
		Long excelTotalAmount = new BigDecimal(rowData[0]).setScale(2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).longValue();
		Integer excelTotal = Double.valueOf(rowData[1]).intValue();
		if(excelTotalAmount.longValue() != totalAmount.longValue()) {
			res += "[批总金额]与系统统计不符合";
		}
		if(excelTotal.intValue() != total.intValue()) {
			res += "\t[批总笔数]与系统统计不符合";
		}
		return res;
	}

}
